package cn.edu.nju.newsFeed;

import java.util.List;

import cn.edu.nju.R;

import android.app.Activity;
import android.content.Intent;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class NewsFeedListAdapter extends BaseAdapter{
	private Activity context;
	private ListItemFormatHelper helper;
	private NewsFeedProvider provider;
	private List<NewsFeedItem> items;	
	private View invalidView;
	private FeedClickableSpan likeSpan;
	private FeedClickableSpan commentSpan;

	public NewsFeedListAdapter(NewsFeedActivity activity){
		this.context = activity;
		this.helper = new ListItemFormatHelper(activity);
		this.provider = NewsFeedProvider.getInstance(activity);
		this.items = provider.getNewsFeedItems();
		initTextSpans();
	}

	private void initTextSpans(){
		likeSpan = new FeedClickableSpan(context) {			
			@Override
			public void onClick(View widget) {
				int position = (Integer)widget.getTag(R.id.VIEW_POSITION_TAG);
				NewsFeedItem item = getItem(position);
				item.addOneLike();
				helper.setLikeNumberText((TextView)widget, item, likeSpan);
			}
		};
		
		commentSpan = new FeedClickableSpan(context){
			@Override
			public void onClick(View widget) {
				invalidView = (View)widget.getTag(R.id.PARENT_VIEW_TAG);
				int position = (Integer)widget.getTag(R.id.VIEW_POSITION_TAG);
				Intent intent = new Intent(context, NewsFeedDetailActivity.class);
				intent.putExtra(NewsFeedActivity.FEED_ITEM_POSITION, position);
				context.startActivity(intent);
			}
		};
	}
	
	public int getCount(){
		return items.size();
	}
	
	public NewsFeedItem getItem(int position){
		return items.get(position);
	}
	
	public long getItemId(int position){
		return position;
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		View resultView = null;
		NewsFeedItemCache viewCache = null;
		if(convertView == null){
			LayoutInflater inflater = context.getLayoutInflater();
			resultView = inflater.inflate(R.layout.news_feed_item, null);
			viewCache = new NewsFeedItemCache(resultView);
			resultView.setTag(R.id.VIEW_CACHE_TAG, viewCache);
		} else{
			resultView = convertView;
			viewCache = (NewsFeedItemCache) resultView.getTag(R.id.VIEW_CACHE_TAG);
		}
		
		NewsFeedItem item = getItem(position);
		
		ImageView avatarView = viewCache.getAvatarView();
		helper.setAvatar(avatarView, item);
		
		TextView contentView = viewCache.getContentView();
		contentView.setTag(R.id.VIEW_POSITION_TAG, position);
		helper.setContentText(contentView, item, null, null);
		
		TextView timeView = viewCache.getTimeView();
		helper.setTimeText(timeView, item);
		
		TextView commentsNumberView = viewCache.getCommentsNumberView();
		commentsNumberView.setTag(R.id.VIEW_POSITION_TAG, position);
		commentsNumberView.setTag(R.id.PARENT_VIEW_TAG, resultView);
		commentsNumberView.setMovementMethod(LinkMovementMethod.getInstance());
		helper.setCommentNumberText(commentsNumberView, item, commentSpan);
		
		TextView likeNumberView = viewCache.getLikeNumberView();
		likeNumberView.setTag(R.id.VIEW_POSITION_TAG, position);
		likeNumberView.setMovementMethod(LinkMovementMethod.getInstance());
		helper.setLikeNumberText(likeNumberView, item, likeSpan);
		
		ImageView previewImageView = viewCache.getPreviewImageView();
		previewImageView.setTag(R.id.VIEW_POSITION_TAG, position);
		helper.setPreviewImage(previewImageView, item, true);
		return resultView;
	}
	
	public void updateInvalidView(){
		if(invalidView != null){
			NewsFeedItemCache viewCache = (NewsFeedItemCache) invalidView.getTag(R.id.VIEW_CACHE_TAG);
			TextView commentsNumberView = viewCache.getCommentsNumberView();
			int position = (Integer)commentsNumberView.getTag(R.id.VIEW_POSITION_TAG);
			
			NewsFeedItem item = getItem(position);
			helper.setCommentNumberText(commentsNumberView, item, commentSpan);
			
			TextView likeNumberView = viewCache.getLikeNumberView();
			helper.setLikeNumberText(likeNumberView, item, likeSpan);
			
			invalidView = null;
		}
	}
	
	public void refresh(){
		// provider will retrieve the newest items
		this.items = provider.getNewsFeedItems();
		notifyDataSetChanged();
	}
}
